/*********************************************************************************************************************
版权所有 (C), 2021,
设计：山亦青
编码：山亦青
版本：V1.0.0.0
完成日期：2021.4.1
说明:
 渐变底色按钮的头文件
代码级别：u
*********************************************************************************************************************/

#include "GradientButton.h"
#include <QStyle>
#include <QPainter>

GradientButton::GradientButton():
    m_IsDown(Bool_false),
    m_BorderRaidus(0),
    m_textSize(10),
    m_IsDisabled(Bool_false)
{
    connect(this, SIGNAL(pressed()), this, SLOT(onButtonDown()));
    connect(this, SIGNAL(released()), this, SLOT(onButtonUp()));
}

GradientButton::GradientButton(QWidget *parent):
    QPushButton(parent),
    m_IsDown(Bool_false),
    m_BorderRaidus(0),
    m_textSize(10),
    m_IsDisabled(Bool_false)
{
    connect(this, SIGNAL(pressed()), this, SLOT(onButtonDown()));
    connect(this, SIGNAL(released()), this, SLOT(onButtonUp()));
}

Void_t GradientButton::setBorderRadius(Length_t radius)
{
    m_BorderRaidus = radius;
}

Void_t GradientButton::setFontSize(Int32_t fontSize)
{
    m_textSize = fontSize;
}

Void_t GradientButton::setDisabled(Bool_t isDisabled)
{
    m_IsDisabled = isDisabled;
    QPushButton::setDisabled(isDisabled);
}

Void_t GradientButton::paintEvent(QPaintEvent* e)
{
    // 重画自己
    QPainter painter(this);
    painter.setRenderHints(QPainter::Antialiasing | QPainter::TextAntialiasing);
    QLinearGradient g(0, 0, 0, height());

    //如果是不使能的样式
    if (m_IsDisabled)
    {
        g.setColorAt(0, QColor(0X808080));
        g.setColorAt(1, QColor(0X808080));
    }
    // 如果是使能
    else if (m_IsDown)
    {
        // 按下的按钮样式
        g.setColorAt(0, QColor(0X001F45));
        g.setColorAt(0.5, QColor(0X103A6D));
        g.setColorAt(1, QColor(0X001F45));
    }
    else
    {
        // 没有按下的按钮样式
        g.setColorAt(0, QColor(0X1C5EB0));
        g.setColorAt(0.5, QColor(0X103A6D));
        g.setColorAt(1, QColor(0X1C5EB0));
    }

    painter.setBrush(g);
    painter.setPen(Qt::NoPen);
    QRect rect(1, 1, width() - 2, height() - 2);
    painter.drawRoundedRect(rect, m_BorderRaidus, m_BorderRaidus);

    QFont font = painter.font();
    font.setPointSize(m_textSize);
    painter.setPen(Qt::white);
    painter.setFont(font);
    painter.drawText(rect, Qt::AlignCenter, text());
}

void GradientButton::onButtonDown()
{
    if (m_IsDisabled)
    {
        return;
    }
    m_IsDown = Bool_true;
    repaint();
    emit onMouseDown(this);
}

void GradientButton::onButtonUp()
{
    if (m_IsDisabled)
    {
        return;
    }
    m_IsDown = Bool_false;
    repaint();
    emit onMouseUp(this);
    emit onMouseClick(this);
}

GradientButton::~GradientButton()
{ }
